<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Personal extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'escort-personal';
	protected $_ci;
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('be_escort');
		$this->lang->load('be_escort_personal');
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index($id)
	{
		$id OR redirect('admin/be_escort');
		
		$escort = $this->_ci->escort_entity->init('id', $id);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		if(!$lockedEscort){
			$this->_ci->locked_escort_entity->createNewLockedEscortEntity($id);
			$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		}
		
		if(!$escort || !$user){
			die();
		}
		$this->data['escort'] = $escort;
		$this->data['user'] = $user;
		$this->data['lockedEscort'] = $lockedEscort;
		$this->pyrocache->delete_all('modules_m');
		$this->load->view('admin/personal/index', $this->data);
	}
	public function save(){
		$result = array();
		$result['result'] = true;
		$escortId = $this->input->post('escort_id');
		$escort = $this->_ci->escort_entity->init('id',$escortId);
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $escortId);
		//$user = $this->_ci->user_entity->init('id', $userId);
		//$escort = new CI_Escort_entity();
		$escort->setSex($this->input->post('sex', TRUE));
		$escort->setPublicSex($this->input->post('public_sex', TRUE));
		$escort->setSexualOrientationId($this->input->post('sexual_orientation_id', TRUE));
		$escort->setPublicSexualOrientation($this->input->post('public_sexual_orientation', TRUE));
		//$escort->setBirthDate(convertToTimeStampFromFormat($this->input->post('birth_date', TRUE),DATE_FORMAT));
		$escort->setBirthDate(strtotime($this->input->post('birth_date')));
		$escort->setPublicBirthDate($this->input->post('public_birth_date', TRUE));
		$escort->setLanguageIds(isset($_POST['languages']) ? implode(',', $_POST['languages']):'');
		$escort->setPublicLanguageIds($this->input->post('public_language_ids', TRUE));
		$escort->setCountryId($this->input->post('country_id', TRUE));
		$escort->setStateId($this->input->post('state_id', TRUE));
		$escort->setBornCountryId($this->input->post('borncountry_id', TRUE));
		if($this->input->post('province_id')){
			$escort->setProvinceId($this->input->post('province_id', TRUE));
		}else{
			$escort->setProvinceId(0);
		}
		if($this->input->post('city_id', TRUE)){
			$escort->setCityId($this->input->post('city_id', TRUE));
		}else{
			$escort->setCityId(0);
		}
		$escort->setPublicCountry($this->input->post('public_country', TRUE));
		//$escort->setBreastsSizeId($this->input->post('personal_info_breasts_size_id', TRUE));
		//$escort->setPublicBreastsSize($this->input->post('public_breasts_size', TRUE));
		$escort->setPenisSizeId($this->input->post('personal_info_penis_size_id', TRUE));
		$escort->setPublicPenisSize($this->input->post('public_penis_size', TRUE));
		$escort->setProfessionId($this->input->post('profession_id', TRUE));
		$escort->setPublicProfession($this->input->post('public_profession', TRUE));
		$escort->setPublicBorncountry($this->input->post('public_borncountry'));
		
		$escort->adminUpdate();

		//$lockedEscort = new CI_Locked_escort_entity();
		$lockedEscort->setLockedSex($this->input->post('locked_sex'));
		$lockedEscort->setLockedSexualOrientation($this->input->post('locked_sexual_orientation'));
		$lockedEscort->setLockedBirthDate($this->input->post('locked_birth_date'));
		$lockedEscort->setLockedLanguageIds($this->input->post('locked_language_ids'));
		$lockedEscort->setLockedCountry($this->input->post('locked_country'));
		//$lockedEscort->setLockedBreastsSize($this->input->post('locked_country'));
		//$lockedEscort->setLockedPenisSize($this->input->post('locked_country'));
		$lockedEscort->setLockedProfession($this->input->post('locked_profession'));
		$lockedEscort->setLockedBornCountry($this->input->post('locked_borncountry'));
		$lockedEscort->update();
		
		$language_array_code = array_filter(explode(',',isset($_POST['languages']) ? implode(',', $_POST['languages']):''));
		foreach($language_array_code as $code){
			if(isset($_POST["language_$code"])){
				$description = $this->input->post("language_$code",TRUE);
				$escort->setDescription($description, $code, $escortId, time());
			}
		}
		
		$this->_ci->escort_description_entity->autoTranslate($escortId);
		$result['message'] = lang('escort_be_escort_personal_update_success');
		echo json_encode($result);
	}
}